#include "SceneLogin.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "deprecated/CCDeprecated.h"
#include "ui/UIHelper.h"
#include "base/CCConsole.h"

#include "GLGlobalProtocol.h"
#include "logic/LogicData.h"
#include "logic/LLogicLogin.h"
#include "scene/SceneManager.h"

USING_NS_CC;
using namespace ui;

CSceneLogin::CSceneLogin(void) : m_LoginNode(nullptr)
{
}


CSceneLogin::~CSceneLogin(void)
{
}

bool CSceneLogin::init() {
	CCLOG("-----------init--------------");
	// 1. super init first
    if ( !CSceneBase::init() )
    {
        return false;
    }

	this->SetReportErrFunc(CC_CALLBACK_1(CSceneLogin::onReportError, this));
    
	m_LoginNode = CSLoader::createNode("login.csb");
	
	ui::Button* tLoginB = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_LoginNode, 93);
	tLoginB->addTouchEventListener(this, toucheventselector(CSceneLogin::onLogin));
	tLoginB = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_LoginNode, 94);
	tLoginB->addTouchEventListener(this, toucheventselector(CSceneLogin::onRegister));

	ui::TextField* tAccount = (ui::TextField*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_LoginNode, 98);
	ui::TextField* tPassword = (ui::TextField*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_LoginNode, 102);
	tAccount->setString("test1");
	tPassword->setString("123123");

    addChild(m_LoginNode);

    return true;
}

void CSceneLogin::onEnter() {
	cocos2d::Scene::onEnter();
}
void CSceneLogin::onExit() {
	cocos2d::Scene::onExit();
}

void CSceneLogin::update(float delta) {
	cocos2d::Scene::update(delta);
}

void CSceneLogin::onRegister(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	ui::Text* tError = (ui::Text*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_LoginNode, 15);
	tError->setText("");

	switch (type) {
	case cocos2d::ui::TOUCH_EVENT_ENDED: {
		CCLOG("-----------onRegister-------------");
		ui::TextField* tAccount = (ui::TextField*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_LoginNode, 98);
		ui::TextField* tPassword = (ui::TextField*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_LoginNode, 102);

		CTLLogicLogin tLogicLogin;
		tLogicLogin.Register(tAccount->getString(), tPassword->getString());
		} break;
	}
}

void CSceneLogin::onLogin(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	ui::Text* tError = (ui::Text*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_LoginNode, 15);
	tError->setText("");

	switch (type) {
	case cocos2d::ui::TOUCH_EVENT_ENDED: {
		CCLOG("-----------onLogin-------------");
		ui::TextField* tAccount = (ui::TextField*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_LoginNode, 98);
		ui::TextField* tPassword = (ui::TextField*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_LoginNode, 102);

		CTLLogicLogin tLogicLogin;
		tLogicLogin.Login(tAccount->getString(), tPassword->getString());
		} break;
	}
}

void CSceneLogin::onReportError(const std::string& err) {
	ui::Text* tError = (ui::Text*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_LoginNode, 15);
	tError->setText(err);
}
